//
//  WLINTChangeDetailController.m
//  WanLian
//
//  Created by muzhuang on 2018/12/4.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLINTChangeDetailController.h"

@interface WLINTChangeDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView   *tbExchargeInfo;
@property (nonatomic, copy) NSDictionary  *dictInfo;

@end

@implementation WLINTChangeDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"兑换详情";
    
    [self reqScoreDetail];
    
    _tbExchargeInfo = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tbExchargeInfo.backgroundColor = wlUIColorRGB(0xFAFAFA);;
    _tbExchargeInfo.delegate = self;
    _tbExchargeInfo.dataSource = self;
    if (@available(iOS 11.0, *)) {
        _tbExchargeInfo.estimatedSectionHeaderHeight = 0;
        _tbExchargeInfo.estimatedSectionFooterHeight = 0;
    }
    [self.view addSubview:_tbExchargeInfo];
    _tbExchargeInfo.scrollEnabled = NO;
    WLWkSelf
    [_tbExchargeInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
}

- (void)reqScoreDetail
{
    WLWkSelf
    NSDictionary *paramsp = @{@"shid":@(_scoreModel.sId)};
//        NSDictionary *paramsp = @{@"conditions":conditions};
    [WLHttpManager WLRequestMethod:RequestTypePost urlStr:@"score/app/score/intdetail" withParamsDic:paramsp success:^(NSDictionary *successDict) {
        if ((![successDict[@"object"] isKindOfClass:[NSNull class]])) {
            weakSelf.dictInfo = successDict[@"object"];
            [weakSelf.tbExchargeInfo reloadData];
        }
    } failure:^(NSString *errorMsg, NSInteger errorTag) {
        [weakSelf showHUDMessage:errorMsg];
    } error:^(NSError *error) {
        
    } inView:self.view];
}

#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return WLScaleX(10);
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return WLScaleX(116);
    }
    else if (indexPath.row==0)
    {
        NSString *strText = _dictInfo[@"wallet"];
        if (strText != nil) {
            CGSize size = [strText boundingRectWithSize:CGSizeMake(WIDTH_SCREEN_WL-WLScaleX(130), 100) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [WLCommonTool fontTypePingFangSCRegular:17]} context:nil].size;
            if (size.height+WLScaleX(24)<=54) {
                return WLScaleX(54);
            }
            return WLScaleX(24)+size.height;
        }
        return WLScaleX(80);
    }
    else if (indexPath.row==4)
    {
        NSString *strText = _dictInfo[@"ethtx"];
        if (strText != nil&&[strText isKindOfClass:[NSNull class]]==NO) {
            CGSize size = [strText boundingRectWithSize:CGSizeMake(WIDTH_SCREEN_WL-WLScaleX(130), 100) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [WLCommonTool fontTypePingFangSCRegular:17]} context:nil].size;
            if (size.height+WLScaleX(24)<=54) {
                return WLScaleX(54);
            }
            return WLScaleX(24)+size.height;
        }
        return WLScaleX(106);
    }
    else
    {
        return WLScaleX(54);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        static NSString *strTop = @"topInfo";
        UITableViewCell *topCell = [tableView dequeueReusableCellWithIdentifier:strTop];
        if (!topCell) {
            topCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strTop];
            topCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        for (UIView *subView in topCell.contentView.subviews) {
            [subView removeFromSuperview];
        }
        
        UILabel *labType = [[UILabel alloc] initWithFrame:CGRectMake(50, WLScaleX(12), WIDTH_SCREEN_WL-100, WLScaleX(25))];
        labType.font = [WLCommonTool fontTypePingFangSCRegular:16];
        labType.textColor = wlBlackColor;
        labType.textAlignment = NSTextAlignmentCenter;
        labType.text = self.scoreModel.stype;
        [topCell.contentView addSubview:labType];
        
        UILabel *labINT = [[UILabel alloc] initWithFrame:CGRectMake(50, WLScaleX(45), WIDTH_SCREEN_WL-100, WLScaleX(35))];
        labINT.font = [WLCommonTool fontTypePingFangSCMedium:24];
        labINT.textColor = wlBlackColor;
        labINT.textAlignment = NSTextAlignmentCenter;
        [topCell.contentView addSubview:labINT];
        NSString *strInt = [NSString stringWithFormat:@"%@ INT",self.dictInfo[@"intnum"]];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:strInt];
        [attStr addAttribute:NSFontAttributeName
                           value:[WLCommonTool fontTypePingFangSCRegular:12]
                           range:NSMakeRange(strInt.length-3, 3)];
        [attStr addAttribute:NSForegroundColorAttributeName
                       value:wlLightTextColor
                       range:NSMakeRange(strInt.length-3, 3)];
        labINT.attributedText = attStr;
        
        UILabel *labStatus = [[UILabel alloc] initWithFrame:CGRectMake(100, WLScaleX(84), WIDTH_SCREEN_WL-200, WLScaleX(20))];
        labStatus.font = [WLCommonTool fontTypePingFangSCRegular:12];
        labStatus.textColor = wlLightTextColor;
        labStatus.textAlignment = NSTextAlignmentCenter;
        [topCell.contentView addSubview:labStatus];
        NSInteger status = [self.dictInfo[@"status"] integerValue];
        if (status==1) {
            labStatus.text = @"已提交";
        }
        else if (status==2)
        {
            labStatus.text = @"兑换中";
        }
        else if (status==3)
        {
            labStatus.text = @"兑换成功";
        }
        else if (status==4)
        {
            labStatus.text = @"兑换失败";
        }
        
        return topCell;
    }
    NSInteger row = indexPath.row;
    static NSString *strCell = @"infoCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:strCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    for (UIView *subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    UILabel *labTitle = [[UILabel alloc] init];//WithFrame:CGRectMake(WLScaleX(16), WLScaleX(12), WLScaleX(90), WLScaleX(30))];
    labTitle.font = [WLCommonTool fontTypePingFangSCRegular:17];
    labTitle.textColor = wlBlackColor;
    [cell.contentView addSubview:labTitle];
    
    UILabel *labDetail = [[UILabel alloc] init];//WithFrame:CGRectMake(WLScaleX(110), WLScaleX(12), WIDTH_SCREEN_WL-WLScaleX(130), WLScaleX(30))];
    labDetail.font = [WLCommonTool fontTypePingFangSCRegular:17];
    labDetail.textColor = wlDarkGrayColor;
    labDetail.numberOfLines = 0;
    [cell.contentView addSubview:labDetail];
    
    if (row==0) {
        labTitle.text = @"接收地址";
//        labDetail.frame = CGRectMake(WLScaleX(110), WLScaleX(12), WIDTH_SCREEN_WL-WLScaleX(130), WLScaleX(60));
        labDetail.text = _dictInfo[@"wallet"];//
    }
    else if (row==1)
    {
        labTitle.text = @"gas费用";
        labDetail.text = [NSString stringWithFormat:@"%@ INT",_dictInfo[@"gas"]];
    }
    else if (row==2)
    {
        labTitle.text = @"实际到账";
        labDetail.text = [NSString stringWithFormat:@"%@ INT",_dictInfo[@"realarrival"]];
    }
    else if (row==3)
    {
        labTitle.text = @"兑换时间";
        labDetail.text = [WLCommonTool timeToYMDHMS:[_dictInfo[@"picktime"] longLongValue]];
    }
    else
    {
        labTitle.text = @"以太坊TX";
//        labDetail.frame = CGRectMake(WLScaleX(110), WLScaleX(12), WIDTH_SCREEN_WL-WLScaleX(130), WLScaleX(90));
        if ([_dictInfo[@"ethtx"] isKindOfClass:[NSNull class]]==NO)
        {
            labDetail.text = _dictInfo[@"ethtx"];
        }
        else
        {
            labDetail.text = @"";
        }
    }
    [labTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.mas_left).offset(WLScaleX(16));
        make.top.equalTo(cell.mas_top).offset((WLScaleX(54)-24)/2.0);
    }];
    [labDetail mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labTitle.mas_top);
        make.left.equalTo(cell.mas_left).offset(WLScaleX(110));
        make.right.equalTo(cell.mas_right).offset(-WLScaleX(20));
    }];
    
    return cell;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
